﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ShadowEngine;
using Tao.OpenGl;
using System.Windows.Forms;
using ShadowEngine.ContentLoading; 

namespace CarRace
{
    class Streetlight
    {
        Position pos;
        int last_state;
        int state; //0 = red, 1 = yellow, 2 = green
        int delay; // in seconds
        int counter = 0;
        public Streetlight(Position _pos, int _state, int _delay)
        {
            pos = _pos;
            state = _state;
            delay = _delay;
        }
        public void Draw()
        {
            counter++;
            if (counter >= delay * 40) // 40 ticks * 25 milliseconds = 1 second
            {
                counter = 0;
                if (state == 1)
                    state = 0;
                else
                    state = 2;

            }
            else if (counter >= delay * 30 && state == 2) // 0.75% - yellow case
            {
                state = 1;
            }

            int textureStreelight;
            switch(state)
            {
                default:
                case 0:
                    textureStreelight = ContentManager.GetTextureByName("redlight.jpg");
                    break;
                case 1:
                    textureStreelight = ContentManager.GetTextureByName("yellowlight.jpg");
                    break;
                case 2:
                    textureStreelight = ContentManager.GetTextureByName("greenlight.jpg");
                    break;
            }
            Gl.glEnable(Gl.GL_TEXTURE_2D);
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, textureStreelight);
            Gl.glPushMatrix();
            Gl.glTranslatef(pos.x, 2, pos.y);
            Gl.glRotatef(-90f, 1.0f, 0, 0);
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3f(0.5f, 0.1f, 0);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3f(0.5f, 0.1f, 1);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3f(0.75f, 0.1f, 1);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3f(0.75f, 0.1f, 0);
            Gl.glEnd();
            Gl.glPopMatrix();
        }
    }
}
